package service

import (
	"fmt"
	"gitee.com/MrDaiM/msgo/orm"
	_ "github.com/go-sql-driver/mysql"
	"net/url"
)

//type User struct {
//	Id       int64  `msorm:"id, auto_increment"`
//	UserName string `msorm:"user_name"`
//	Password string `msorm:"password"`
//	Age      int    `msorm:"age"`
//}

type User struct {
	Id       int
	UserName string
	Password string
	Age      int
}

func SaveUser() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	user := &User{
		//Id:       1000,
		UserName: "leon",
		Password: "123456",
		Age:      20,
	}
	id, _, err := db.New(&User{}).Insert(user)
	fmt.Println("primary id", id)
	if err != nil {
		panic(err)
	}
	err = db.Close()
	if err != nil {
		panic(err)
	}
}

func SaveUserBatch() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	db.Prefix = "msgo_"
	user := &User{
		UserName: "mszlu222",
		Password: "12345612",
		Age:      54,
	}
	user1 := &User{
		UserName: "mszlu111",
		Password: "123456111",
		Age:      12,
	}
	var users []any
	users = append(users, user, user1)
	id, _, err := db.New(&User{}).InsertBatch(users)
	if err != nil {
		panic(err)
	}
	fmt.Println(id)

	db.Close()
}

func UpdateUser() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	//user := &User{
	//	//Id:       1000,
	//	UserName: "leon",
	//	Password: "123456",
	//	Age:      20,
	//}
	id, _, err := db.New(&User{}).Table("msgo_user").Where("id", 1000).Update("age", 90)
	fmt.Println("primary id", id)
	if err != nil {
		panic(err)
	}
	err = db.Close()
	if err != nil {
		panic(err)
	}
}

func UpdateUser1() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	user := User{
		//Id:       1000,
		UserName: "leon",
		Password: "6666",
		Age:      50,
	}
	id, _, err := db.New(&User{}).Where("id", 1000).Where("age", 90).Update(&user)
	fmt.Println("primary id", id)
	if err != nil {
		panic(err)
	}
	err = db.Close()
	if err != nil {
		panic(err)
	}
}
func UpdateUser2() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	//user := User{
	//	//Id:       1000,
	//	UserName: "leon",
	//	Password: "6666",
	//	Age:      50,
	//}
	id, _, err := db.New(&User{}).Table("msgo_user").Where("id", 1000).UpdateParam("age", 101).Update()
	fmt.Println("primary id", id)
	if err != nil {
		panic(err)
	}
	err = db.Close()
	if err != nil {
		panic(err)
	}
}

func SelectOne() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	user := &User{}
	err = db.New(user).Where("id", 1000).SelectOne(user)
	if err != nil {
		panic(err)
	}
	fmt.Println(user)
	err = db.Close()
	if err != nil {
		panic(err)
	}
}
func SelectOne1() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	user := &User{}
	err = db.New(user).Where("id", 1000).Or().Where("age", 101).SelectOne(user)
	if err != nil {
		panic(err)
	}
	fmt.Println(user)
	err = db.Close()
	if err != nil {
		panic(err)
	}
}
func Select() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	user := &User{}
	users, err := db.New(user).Select(user)
	if err != nil {
		panic(err)
	}
	for _, v := range users {
		u := v.(*User)
		fmt.Println(u)
	}
	fmt.Println(users)
	err = db.Close()
	if err != nil {
		panic(err)
	}
}

func Count() {
	dataSourceName := fmt.Sprintf("root:leon@tcp(49.235.66.235:3306)/msgo?charset=utf8&loc=%s&parseTime=true", url.QueryEscape("Asia/Shanghai"))
	db, err := orm.Open("mysql", dataSourceName)
	if err != nil {
		panic(err)
	}
	db.Prefix = "msgo_"
	user := &User{}
	count, err := db.New(user).Count()
	if err != nil {
		panic(err)
	}

	fmt.Println(count)
	err = db.Close()
	if err != nil {
		panic(err)
	}
}
